
const {BannerModel} = require('../../models/index')
const koaTool = require('../../commons/koa_tool')


class BannerController {

    //新增
    create = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, ['name', 'pic'])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }

        let newData = Object.assign({}, input, {
        })

        let detail = await BannerModel.create(newData)
        return koaTool.success(ctx,'创建成功');
    }

    //详情
    detail = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, ['id'])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }
        const detail = await BannerModel.findById(input.id)
        if(detail){
            return koaTool.success(ctx,'获取成功',{
                detail
            });
        }
        return koaTool.error(ctx,'无该信息');
    }



    getBannerBelongMap = async (ctx) => {

        const {BANNER_BELONG_MAP} = require('../../commons/const.js');

        return koaTool.success(ctx,"获取成功",{
            map: BANNER_BELONG_MAP
        })
    }

    getBannerBelongList = async (ctx) => {

        const {BANNER_BELONG_LIST} = require('../../commons/const.js');

        return koaTool.success(ctx,"获取成功",{
            list: BANNER_BELONG_LIST
        })
    }

    getBannerOpenTypeMap = async (ctx) => {

        const {BANNER_OPEN_TYPE_MAP} = require('../../commons/const.js');

        return koaTool.success(ctx,"获取成功",{
            map: BANNER_OPEN_TYPE_MAP
        })
    }

    getBannerOpenTypeList = async (ctx) => {

        const {BANNER_OPEN_TYPE_LIST} = require('../../commons/const.js');

        return koaTool.success(ctx,"获取成功",{
            list: BANNER_OPEN_TYPE_LIST
        })
    }

    //更新
    update = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, ['id'])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }
        let detail = await BannerModel.findById(input.id)
        if (!detail) {
            return koaTool.succ(ctx, {}, 405, '数据不存在')
        }
        const {
            name, pic, order,belong,open_type,open_url
        } = input
        if (name) {
            detail.name = name
        }
        if(pic){
            detail.pic = pic;
        }

        if(order){
            detail.order = order;
        }

        if(belong){
            detail.belong = belong;
        }

        if(open_type){
            detail.open_type = open_type;
        }

        if(open_url){
            detail.open_url = open_url;
        }


        await detail.save()
        return koaTool.success(ctx, '编辑成功');
    }
    //删除
    delete = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, ['id'])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }
        const detail = await BannerModel.findById(input.id);
        await detail.remove();
        return koaTool.success(ctx, '删除成功');
    }
    //列表
    list = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, ['page'])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }
        let {
            page,
            limit,
            belong,
        } = ctx.request.query;

        const {isNull} = require('../../commons/util.js');



        limit = Number(limit)
        page = page || 0
        page = page - 1
        let query = {
        };


        if(!isNull(belong)){
            query['belong'] = belong;
        }

        let total = await BannerModel.countDocuments(query)
        let list = await BannerModel.find(query).sort([
            ['order', 1]
        ]).skip(page * limit).limit(limit);

        koaTool.succ(ctx, {
            list,
            total
        })
    }

}

module.exports=new BannerController()